package cn.xxy.result;

import cn.xxy.ResultSetHandler;
import cn.xxy.RowProcessor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author xxy
 * @date 2021/7/29
 */
public class ColumnListHandler<T> extends  org.apache.commons.dbutils.handlers.AbstractListHandler<T> implements ResultSetHandler<List<T>> {

    private Integer columnIndex;
    private String columnName;
    public ColumnListHandler() {
        this(1);
    }
    public ColumnListHandler(int columnIndex) {
        this.columnIndex = columnIndex;
    }

    public ColumnListHandler(String columnName) {
        this.columnName = columnName;
    }

    @SuppressWarnings("unchecked")
    @Override
    public T handleRow(ResultSet rs) throws SQLException {
        if(this.columnName == null) {
            return (T) RowProcessor.toValue(rs, this.columnIndex);
        }
        return (T) RowProcessor.toValue(rs, this.columnName);
    }
}
